package com.itheima.health.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Component
public class ZookeeperConfig {
    @Bean
    public InterProcessMutex getMutex(){
        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retry);
        client.start();
        InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock");
        /*mutex.acquire();
        System.in.read();
        mutex.release();
        client.close();*/
        return mutex;
    }
}
